﻿using PidKeyReptile.Desktop.Extend;
using PidKeyReptile.Desktop.Helper;
using PidKeyReptile.Desktop.Model;
using PidKeyReptile.Desktop.Pages.Checker;
using PidKeyReptile.Desktop.Pages.Crawler;
using Stylet;
using StyletIoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PidKeyReptile.Desktop.CrawlTask
{
    public class Aihao_CC : GetWeb, ICapture
    {
        public Aihao_CC(CrawlerManagerViewModel Crawler):base(Crawler)
        {
        }

        public async Task CaptureAsync(string url)
        {
            string cookie = AppConfigurtaionServices.Configuration["Cookie:aihao"];
            try
            {
                //首页数据
                var docIndex = GetHtml(url, cookie);
                //新文章列好
                var newArticle = docIndex.DocumentNode.SelectNodes("//*[@id='category_grid']/table/tr/td")[0].SelectNodes("div/ul/li");
                foreach (var li in newArticle)
                {
                    string articleUrl = li.SelectSingleNode("a").Attributes["href"].Value.Replace("&amp;", "&");
                    string articleUrl2 = await JavaScripInvok.InovkAsync(articleUrl);
                    var articleHtml = GetHtml(articleUrl2, cookie);

                    var txt = articleHtml.DocumentNode.SelectNodes("//td[@class='plc']/div[@class='pct']")?[0]?.InnerText;
                    if (txt == null) continue;
                    await ParseAsync(txt);
                }
            }
            catch (Exception ex)
            {
                _crawler.LogText += ($"{DateTime.Now}采集错误{ex.Message}\r\n");
                Console.WriteLine($"{DateTime.Now}采集错误{ex.Message}");
            }
            if (KeyList.Count == 0) return;
            int xx = await Db.Insertable(KeyList).ExecuteCommandAsync();
            _crawler.LogText+= $"成功入库：{xx}条数据\r\n";
            Console.WriteLine($"成功入库：{xx}条数据");
        }

    }
}
